﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using memoVM;
using memo;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for ContentEditorWindow.xaml
    /// </summary>
    public partial class ContentEditorWindow : Window
    {
        public ContentEditorWindow(bool isNew)
        {
            IsNew = isNew;

            InitializeComponent();

            _ViewModel = ViewModel.Instance;

            Init();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if ((NameTextBox.Text != string.Empty) || (!string.IsNullOrWhiteSpace(NameTextBox.Text)))
            {
                if (IsNew == true)
                {
                    _ViewModel.SaveContent(NameTextBox.Text, DetailTextBox.Text);
                }
                else
                {
                    _ViewModel.EditContent(NameTextBox.Text, DetailTextBox.Text);
                }
                
                this.Close();
            }
            else
            {
                MessageBox.Show("ContentName (╯°□°）╯︵ ┻━┻");
            }
        }

        public void Init()
        {
            if (IsNew == true)
            {
                NameTextBox.Text = "";
                DetailTextBox.Text = "";
            }
            else
            {
                myContent = _ViewModel.GetSelectedContent();
                NameTextBox.Text = myContent.Title;
                DetailTextBox.Text = myContent.Details;
            }
        }

        private bool IsNew;
        private ViewModel _ViewModel;
        private Content myContent;
    }
}
